/**      
 * @file			two_dimensions.cpp
 * @description		This program reads a file specified by the user
 *					and determines the longest line, the line number
 *					of the longest line, and the total lines in the
 *					file. Then it outputs that data to the user.
 * @course			CSCI 123 Section 00000
 * @assignment		
 * @date			3/7/2006
 * @author			Brad Rippe (00000000) brippe@fullcoll.edu
 * @version			1.0
 */
#include <iostream>		// cin and cout
#include <fstream>		// file input and output
#include <cstdlib>		// exit() 
#include <string>		// strcpy_s()
using namespace std;

/**
 * Application Constants
 */
const int LINE_SIZE = 300;
const int NAME_SIZE = 80;

/**
 * documentation is left out on purpose
 */
void getFileName(char aFileName[]);

void openFile(ifstream& aInFile, const char aFileName[]);

void getTeams(string aTeams[][2], int aWinsLosses[][2], ifstream& aInFile, int& aTeamSize);

void cleanScore(char aScore[]);

void getATeam(ifstream& aInFile, string aTeams[][2], 
			  int aTeamSize, int aWinsLosses[][2], int& aTeamIndex);

void outputTeamsWScores(const string aTeams[][2], const int aWinsLosses[][2], 
						const int aTeamSize);

void outputTeamRecord(const string aTeam, const string aTeams[][2], const int aTeamSize);

/**
 * @return zero if the application executes successfully
 */
int main() {

	char fileName[NAME_SIZE];
	string teams[550][2];
	int winsLosses[550][2];
	int teamSize				= 0;
	ifstream inFile;

	getFileName(fileName);
	openFile(inFile, fileName);	
	getTeams(teams, winsLosses, inFile, teamSize);
	//outputTeamsWScores(teams, winsLosses, teamSize);
	outputTeamRecord("Jazz", teams, teamSize);
	inFile.close();

	return 0;
}

void getFileName(char aFileName[]) {
	cout << "Please type the name of your input file.\n";
	cin >> aFileName;
}

void openFile(ifstream& aInFile, const char aFileName[]) {
	aInFile.open(aFileName);
	if(aInFile.fail()) {
		cout << "Couldn't open " << aFileName << " for input.\n";
		exit(1);
	}
}

void cleanScore(char aScore[]) {
	int i = 0;
	while(aScore[i] != '\0') {
		if(aScore[i] == ',') {
			aScore[i] = '\0';
			break;
		}
		i++;
	}
}

void getATeam(ifstream& aInFile, string aTeams[][2], 
			  int aTeamSize, int aWinsLosses[][2], int aTeamIndex) {
	
	string fileData;
	char score[20];
	// get the first team
	aInFile >> fileData;
	aTeams[aTeamSize][aTeamIndex] = fileData;
	aInFile >> score;
	cleanScore(score);
	aWinsLosses[aTeamSize][aTeamIndex] = atoi(score);
}

void getTeams(string aTeams[][2], int aWinsLosses[][2], ifstream& aInFile, int& aTeamSize) {
	string fileData;
	
	while(aInFile >> fileData) {
		if(fileData == "Title:") {
			// get the first team
			getATeam(aInFile, aTeams, aTeamSize, aWinsLosses, 0);
			// get the second team
			getATeam(aInFile, aTeams, aTeamSize, aWinsLosses, 1);
			aTeamSize++;
		}
	}
}

void outputTeamsWScores(const string aTeams[][2], const int aWinsLosses[][2], const int aTeamSize) {
	for(int i = 0; i < aTeamSize; i++) {
		cout << aTeams[i][0] << " vs " << aTeams[i][1] << endl;
		cout << "  " << aWinsLosses[i][0] 
			 << " to " << aWinsLosses[i][1] << endl;
	}
}

void outputTeamRecord(const string aTeam, const string aTeams[][2], const int aTeamSize) {
	int totalWins = 0;
	int totalLosses = 0;
	for(int i = 0; i < aTeamSize; i++) {
		if(aTeams[i][0] == aTeam)
			totalWins++;
		else if(aTeams[i][1] == aTeam)
			totalLosses++;
	}
	cout << "The " << aTeam << "'s record is " << totalWins
		 << " and " << totalLosses << endl;
}
